<?php
class SaveMsgAction extends Action {
	
	function saveImgmsg() {
		
		$id = $_REQUEST['id'];
		
		if(empty($id)){
			return;
		}
		
		$model = M('wxmsg');
		$where['id'] = $id;
		$msg = $model->where($where)->field('id,msgcontent,openid')->find();
		
		$picPath = $msg['msgcontent'];
		
		Log::write('收到图片消息，准备保存图片：'.$picPath, LOG::INFO);
		
		if(stristr($picPath,'http') === false){
			return;
		}
		
		$picdata = file_get_contents ( $picPath );
		$filetime = time ();
		
		$filepath = './imgmsg/';
		
		if (! is_dir ( $filepath )) {
			mkdir ( $filepath, '0777' );
		}
		
		$filename = $msg['openid']."_$filetime.jpg";
		$fp = @fopen ( $filepath . $filename, 'w' );
		@fwrite ( $fp, $picdata );
		fclose ( $fp );
		$msg ['msgcontent'] = $filename;
		$model->save($msg);
		Log::write('已保存图片：'.$filename, LOG::INFO);
	}
}